/**
 * 
 */
package org.gocom.components.coframe.tools;

import java.io.IOException;

import javax.servlet.Filter;     
import javax.servlet.FilterChain;     
import javax.servlet.FilterConfig;     
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;     
import javax.servlet.ServletRequest;     
import javax.servlet.ServletResponse;     
import javax.servlet.http.HttpServletRequest;     
import javax.servlet.http.HttpServletResponse;     
import javax.servlet.http.HttpSession;     

import org.apache.commons.lang.StringUtils;     

import com.eos.data.datacontext.DataContextManager;
import com.eos.system.annotation.Bizlet;

/**
 * @author 唐能
 * @date 2018-06-25 14:27:23
 *
 */
@Bizlet("域名登入")
public class loginZhaoPing implements Filter{
	
	/**   
	* 需要排除的页面   
	*/    
	  
	private String excludedPages;       
	private String[] excludedPageArray;     

	public void destroy() {
		
	}
	/**   
	* 对session进行判断当前访问是否有登录   
	*/   
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		boolean isExcludedPage = false;     
		if(excludedPageArray!=null){
			for (String page : excludedPageArray) {//判断是否在过滤url之外     
				if(((HttpServletRequest) request).getServletPath().equals(page)){     
					isExcludedPage = true;     
					break;     
				}     
			}     
		}
		  
		if (isExcludedPage) {//在过滤url之外     
		chain.doFilter(request, response);     
		} else {//不在过滤url之外，判断session是否存在     
		//获取当前的工号
			
		HttpSession session = ((HttpServletRequest) request).getSession(); 
		String empCode = null;
		String ContextPath = null;
		if(DataContextManager.current().getMUODataContext().getUserObject()!=null){
			empCode = DataContextManager.current().getMUODataContext().getUserObject().getUserId();
			ContextPath = com.eos.foundation.eoscommon.BusinessDictUtil.getDictName("recruit_url","ContextPath");
		}else{
			ContextPath = "/";
		}
		if (session == null || empCode==null||empCode==""){  
			
		((HttpServletResponse) response).sendRedirect(ContextPath+"coframe/auth/zhaopin/login2.jsp");     
		} else {     
			((HttpServletResponse) response).sendRedirect(ContextPath+"home/zhaopin/recruit_index.jsp");
			}     
		}  
		
	}
	
	/**   
	* 初始化函数，获取需要排除在外的url   
	*/    
	public void init(FilterConfig fConfig) throws ServletException {
		excludedPages = fConfig.getInitParameter("excludedPages");     
		if (StringUtils.isNotEmpty(excludedPages)) {     
		excludedPageArray = excludedPages.split(",");     
		}     
		return;     
		
	}
	
	

}
